package com.mofang.ticketing.common.mq.rabbit.config.voucher;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LGC
 */
@Configuration
public class VoucherRabbitConfig {

    public static final String VOUCHER_EXCHANGE = "voucher.exchange";
    public static final String VOUCHER_DELAY_EXCHANGE = "voucher.delay.exchange";

    public static final String VOUCHER_CREATE_QUEUE = "voucher.create.queue";
    public static final String VOUCHER_EXPIRE_REFUND_DELAY_QUEUE = "voucher.expire.refund.delay.queue";


    @Bean
    public Exchange voucherExchange() {
        return new DirectExchange(VOUCHER_EXCHANGE);
    }

    @Bean
    public CustomExchange voucherDelayExchange() {
        Map<String, Object> args = new HashMap<>(2);
        args.put("x-delayed-type", "direct");
        return new CustomExchange(VOUCHER_DELAY_EXCHANGE, "x-delayed-message", true, false, args);
    }


    /*************************************凭证生成*******************************************/
    @Bean
    public Queue voucherCreateQueue() {
        return new Queue(VoucherBindEnum.VOUCHER_CREATE.getQueue());
    }

    @Bean
    public Binding voucherCreateBinding() {
        return BindingBuilder
                .bind(voucherCreateQueue())
                .to(voucherExchange())
                .with(VoucherBindEnum.VOUCHER_CREATE.getKey()).noargs();
    }


    /*************************************凭证过期可退*******************************************/

    @Bean
    public Queue voucherExpireRefundDelayQueue() {
        return new Queue(VoucherBindEnum.VOUCHER_EXPIRE_REFUND_DELAY.getQueue());
    }

    @Bean
    public Binding voucherExpireRefundDelayBinding() {
        return BindingBuilder
                .bind(voucherExpireRefundDelayQueue())
                .to(voucherDelayExchange())
                .with(VoucherBindEnum.VOUCHER_EXPIRE_REFUND_DELAY.getKey()).noargs();
    }


}
